<template>
  <common-list>
    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <el-form
        @submit.native.prevent
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="95px"
      >
        <el-form-item label="资产分组名称" prop="name">
          <common-input
            :verify="false"
            v-model="queryParams.name"
            clearable
            :maxlength="16"
            @enter="handleQuery"
          />
        </el-form-item>
        <!-- <el-form-item label="描述" prop="description">
          <common-input
            v-model="queryParams.description"
            placeholder="请输入"
            clearable

            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
      </el-form>
    </common-search>

    <!--    <el-row :gutter="10" class="mb8" slot="tools">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              @click="handleAdd"
              v-hasPermi="['apisecurity:group:add']"
            >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['apisecurity:group:remove']"
            >批量删除
            </el-button>
          </el-col>
        </el-row>-->

    <common-table
      slot="content"
      v-loading="loading"
      :data="groupList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="left" prop="id" /> -->
      <el-table-column
        label="资产分组名称"
        align="left"
        prop="name"
        show-overflow-tooltip
      />
      <!--      <el-table-column label="应用管理名称" align="left" prop="licationname" />-->
      <el-table-column
        label="描述"
        align="left"
        prop="description"
        show-overflow-tooltip
      />
      <!--      <el-table-column label="操作" align="left" class-name="small-padding fixed-width">-->
      <el-table-column label="操作" align="left" width="75">
        <template slot-scope="scope">
          <!-- <el-button  type="text" v-hasPermi="['apisecurity:group:sort']">资产拓扑
          </el-button> -->
          <template v-if="scope.row.id != '1'">
            <!--            <el-button
                          type="text"
                          @click="handleUpdate(scope.row)"
                          v-hasPermi="['apisecurity:group:edit']"
                        >
                          编辑
                        </el-button>
                        <el-button
                          type="text"
                          @click="handleDelete(scope.row)"
                          v-hasPermi="['apisecurity:group:remove']"
                        >删除
                        </el-button>-->
            <el-button
              type="text"
              @click="topology(scope.row)"
              v-hasPermi="['apisecurity:group:tree']"
            >
              资产拓扑
            </el-button>
          </template>
        </template>
      </el-table-column>
    </common-table>

    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或编辑资产分组对话框 -->
    <common-edit-dialog
      :title="title"
      :visible.sync="open"
      @cancel="cancel"
      @submit="submitForm"
      :buttonLoading="buttonLoading"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
        @submit.native.prevent
      >
        <el-row :gutter="18">
          <el-col :span="24">
            <el-form-item label="资产分组名称" prop="name">
              <common-input v-model="form.name" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <!-- <el-form-item label="应用管理名称" prop="aplManageId">
            <el-select placeholder="请选择" v-model="form.aplManageId" clearable>
              <el-option
                v-for="(item, id) in licationList"
                :key="id"
                :label="item.applicationName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item> -->
          <el-col :span="24">
            <el-form-item label="描述" prop="description">
              <common-input
                v-model="form.description"
                type="textarea"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </common-edit-dialog>

    <template v-if="isTopologyDialog">
      <topology :groupName="groupName" @close="closeTopologyDialog"></topology>
    </template>
  </common-list>
</template>

<script>
import {
  listGroup,
  getGroup,
  delGroup,
  addGroup,
  updateGroup,
} from "@/api/apisecurity/apigroup/group";
import BaseList from "@/mixins/BaseList";
import topology from "./topology";

export default {
  name: "Group",
  components: {
    topology,
  },
  mixins: [BaseList],
  data() {
    return {
      isTopologyDialog: false,
      groupName: "",
      // aplManageId: "",
      licationList: [],

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示查询条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资产分组表格数据
      groupList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      sensitiveName: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        // description: null,
        // status: null,
      },
      // 表单参数
      form: {},
      buttonLoading: false,
      // 表单校验
      rules: {
        name: [
          { required: true, message: "请输入资产分组名称", trigger: "blur" },
        ],
        description: [
          { max: 256, message: "长度不能超过256", trigger: "blur" },
        ],
        // aplManageId: [
        //   { required: true, message: "请选择名称", trigger: "blur" },
        // ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询资产分组列表 */
    getList() {
      this.loading = true;
      // listLication().then((res) => {
      //   this.licationList = res.rows;
      listGroup(this.queryParams)
        .then((res) => {
          if (res && res.code === 200) {
            this.groupList = res && res.rows ? res.rows : [];
            this.total = res && res.total ? res.total : 0;
            // this.licationList.forEach((v) => {
            //   this.groupList.forEach((item) => {
            //     if (v.id == item.aplManageId) {
            //       item.licationname = v.applicationName;
            //     }
            //   });
            // });
          } else {
            this.groupList = [];
            this.total = 0;
            this.handleListTips(res);
          }
        })
        .finally(() => {
          this.loading = false;
        });
      // });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        description: null,
        status: 0,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 查询按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = this.$options.data().queryParams;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增资产分组";
    },
    /** 编辑按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getGroup({ id }).then((res) => {
        if (res && res.code === 200) {
          this.form = res.data;
          this.open = true;
          this.title = "编辑资产分组";
        } else {
          this.$GlobalErrorConfirm("获取数据失败", res.msg, "重新加载").then(
            () => {
              this.handleUpdate(row);
            }
          );
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateGroup(this.form)
              .then((res) => {
                if (res && res.code === 200) {
                  this.$GlobalSuccessMessage("编辑成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.$GlobalErrorConfirm("编辑失败", res.msg);
                }
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addGroup(this.form)
              .then((res) => {
                if (res && res.code === 200) {
                  this.$GlobalSuccessMessage("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.$GlobalErrorConfirm("新增失败", res.msg);
                }
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$GlobalDeleteConfirm()
        .then(() => {
          delGroup({ ids: row.id ? row.id : ids.join(",") }).then((res) => {
            if (res && res.code == 200) {
              this.queryParams.pageNum = 1;
              this.$GlobalSuccessMessage("删除成功");
              if (
                this.queryParams.pageNum > 1 &&
                ids.split(",").length === this.groupList.length
              ) {
                this.queryParams.pageNum -= 1;
              }
              this.getList();
            } else {
              this.$GlobalErrorConfirm("删除失败", res.msg).then();
            }
          });
        })
        .catch(() => {});
    },
    topology(row) {
      // console.log('row: ', row)
      this.groupName = row.name;
      this.isTopologyDialog = true;
    },
    closeTopologyDialog() {
      this.isTopologyDialog = false;
    },
  },
};
</script>
